define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	require("resource/js/component/rules.js");
	require("dcs/section/css/questions.css");
	var AJAX = require("resource/js/component/ajax.js");
	var CONST = require("resource/js/config/const.js");
	var E = require("resource/js/component/easyutil.js");
	var EC = require("resource/js/component/easycombobox.js");
	EC.create({
		url:BASEPATH+"/base/authoriza/basecode/getType.htm",
		param:{checkeds:"status,systype,deptType,con_scheduler"}
	});

	function initPage(){
	    var pid = $('#deptParentId').val();//获取项目ID
		pileKeyCombobox(pid);
	}
	function pileKeyCombobox(pid){
        AJAX.init().sync().success(function(code,msg,data){
        	//获取Data数据
        	EC.init({ ele:"#prileKey_search",valueField:'id',remote:true, textField:'name',
        		onChange: function(valueField) {//调用标段绘图切换
        			if(valueField != ""){
        				previewDialogN(valueField);
        			}else{
        				previewDialogN("0");
        			}
        		}
        	}).load(data);
        	if(data.length > 1){
        		$("#prileKey_search").combobox({
        			value: data[1].name
        		});
        	}
        	if(data.length > 1){
        		previewDialogN(data[1].id);//默认
        	} else {
        		previewDialogN(null);//默认
        	}
        })/*.error(function(code,msg,data){
        	$.messager.alert("系统提示", msg, "error");
        })*/.post(BASEPATH+'/dcs/projectOverviewAction/getPileKeyList.htm?pid='+pid);
	}
	
	
	
	function previewDialogN(sectionId){
		AJAX.init().sync().success(function(code,msg,data){
			var map = new BMap.Map("BMap");
			var point = new BMap.Point(116.404, 39.915);
			map.addControl(new BMap.ScaleControl()); // 添加比例尺控件
			var pointArr1 = null;
			if(data){
				pointArr1 = data;
			}else{
				pointArr1 = [];
			}
			var pointArr=[];
			for (var k = 0; k < pointArr1.length; k += 1) {
				pointArr.push({
					lng: pointArr1[k].longitude,
					lat: pointArr1[k].latitude,
					pile_key:pointArr1[k].pileKey,
					status:pointArr1[k].status
				});

			}
			// 生成坐标点
			var trackPoint = [];
			for (var i = 0, j = pointArr.length; i < j; i++) {
				var point =  new BMap.Point(pointArr[i].lng, pointArr[i].lat);
				point.title = pointArr[i].pile_key;
				point.status = pointArr[i].status;
				trackPoint.push(point);
			}
			map.centerAndZoom(trackPoint[0], 15);
			//开启滑轮滚动
			map.enableScrollWheelZoom();

			// 画线
			var polyline = new BMap.Polyline(trackPoint, {
				strokeColor: "#ad0e21",
				strokeWeight: 1,
				setStrokeStyle:"dashed",
				strokeOpacity: 1
			});
			map.addOverlay(polyline);
			// 配置图片
			var size = new BMap.Size(12, 25);
			var offset = new BMap.Size(0, -11.5);
			var imageSize = new BMap.Size(12, 25);
			var icon = new BMap.Icon(BASEPATH+"/dcs/section/images/ic_tomap3.png", size, {
				imageSize: imageSize,
				infoWindowAnchor:new BMap.Size(0, -5)
			});
			var icon2 = new BMap.Icon(BASEPATH+"/dcs/section/images/ic_tomap4.png", size, {
				imageSize: imageSize,
				infoWindowAnchor:new BMap.Size(0, -5)
			});

			// 画图标
			for (var i = 0, j = trackPoint.length; i < j; i++) {
				if(trackPoint[i].status == 0){
					var marker = new BMap.Marker(trackPoint[i], {
						icon: icon2,
						title:trackPoint[i].title,
						offset: offset
					}); 

				}else{
					var marker = new BMap.Marker(trackPoint[i], {
						icon: icon,
						title:trackPoint[i].title,
						offset: offset
					}); 
				}
				// 创建标注
				map.addOverlay(marker);

			}
			//根据经纬极值计算绽放级别。 (从网上复制)
			function getZoom(maxLng, minLng, maxLat, minLat) {
				var zoom = ["50", "100", "200", "500", "1000", "2000", "5000", "10000", "20000", "25000", "50000", "100000", "200000", "500000", "1000000", "2000000"]; // 级别18到3。
				var pointA = new BMap.Point(maxLng, maxLat); // 创建点坐标A
				var pointB = new BMap.Point(minLng, minLat); // 创建点坐标B
				var distance = map.getDistance(pointA, pointB).toFixed(1); //获取两点距离,保留小数点后两位
				for (var i = 0, zoomLen = zoom.length; i < zoomLen; i++) {
					if (zoom[i] - distance > 0) {
						return 18 - i + 3; //之所以会多3，是因为地图范围常常是比例尺距离的10倍以上。所以级别会增加3。
					}
				}
			}
			//  (从网上复制)
			function setZoom(points) {
				if (points.length > 0) {
					var maxLng = points[0].lng;
					var minLng = points[0].lng;
					var maxLat = points[0].lat;
					var minLat = points[0].lat;
					var res;
					for (var i = points.length - 1; i >= 0; i--) {
						res = points[i];
						if (res.lng > maxLng) maxLng = res.lng;
						if (res.lng < minLng) minLng = res.lng;
						if (res.lat > maxLat) maxLat = res.lat;
						if (res.lat < minLat) minLat = res.lat;
					}
					var cenLng = (parseFloat(maxLng) + parseFloat(minLng)) / 2;
					var cenLat = (parseFloat(maxLat) + parseFloat(minLat)) / 2;
					var zoom = getZoom(maxLng, minLng, maxLat, minLat);
					map.centerAndZoom(new BMap.Point(cenLng, cenLat), zoom);
				} else {
					//没有坐标，显示全中国
					map.centerAndZoom(new BMap.Point(103.388611, 35.563611), 5);
				}
			}
			setZoom(pointArr);
		}).post(BASEPATH+'/dcs/projectOverviewAction/getAllPileKey.htm',{sectionId:sectionId});
		$("#iocImg").show();
	};
	module.exports = {
			trans:function(id){
				using(["datagrid","ztreebox","parser","textbox","messager","dialog","combobox","numberbox"],function(){
					initPage();
				});
			}

	}
});
